import random

from .mole import Mole

class Hole:
    def __init__(self, center):
        self.center = center  # (x, y)
        self.current_mole = None

    def spawn_mole(self, min_ms, max_ms, mole_type=None):
        """Spawn a mole optionally with a specific type.

        If mole_type is None, caller may choose to supply or Hole can randomly
        select a type outside (Game will usually call without type and let
        higher-level logic pick probabilities).
        """
        if self.current_mole is None:
            up_ms = random.randint(min_ms, max_ms)
            mtype = mole_type or 'normal'
            self.current_mole = Mole(up_ms, mtype)
            return True
        return False

    def update(self):
        if self.current_mole is not None:
            if self.current_mole.is_expired():
                self.current_mole = None

    def is_occupied(self):
        return self.current_mole is not None

    def try_hit(self):
        """Attempt to hit the current mole.

        Returns the Mole instance if the hit was successful, otherwise None.
        """
        if self.current_mole:
            ok = self.current_mole.hit()
            if ok:
                return self.current_mole
        return None
